package com.ruoyi.web.controller.project;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.AwardsRule;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Awards;
import com.ruoyi.system.service.IAwardsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.controller.tool.NodeLabelConcatenator;
import org.springframework.web.multipart.MultipartFile;

/**
 * 奖项管理Controller
 *
 * @author xrx
 * @date 2024-05-09
 */
@RestController
@RequestMapping("/project/awards")
public class AwardsController extends BaseController {
    @Autowired
    private IAwardsService awardsService;


    @GetMapping("/getUserAwardsInfo")
    public AjaxResult getUserAwardsInfo() {
        return success(awardsService.getUserAwardsInfo());

    }

    @PostMapping("/importAwards")
    public AjaxResult importAwards(MultipartFile file, boolean updateSupport)throws Exception {
        ExcelUtil<Awards> util = new ExcelUtil<Awards>(Awards.class);
        List<Awards> awardsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = awardsService.importAwards(awardsList, updateSupport, operName);
        return success(message);
    }


    /**
     * 查询奖项管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:awards:list')")
    @GetMapping("/list")
    public TableDataInfo list(Awards awards) {
        startPage();
        Long userId = getUserId();
        List<Awards> list = new ArrayList<>();
        if (getLoginUser().getUserId() == 1) {
            list = awardsService.selectAwardsList(awards);
            if (!list.isEmpty()) {
                Awards awards1 = list.get(0);
            }
        } else {
            list = awardsService.selectAwardsListByUserId(userId);
        }
        return getDataTable(list);
    }

    /**
     * 导出奖项管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:awards:export')")
    @Log(title = "奖项管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Awards awards) {
        List<Awards> list = awardsService.selectAwardsList(awards);
        ExcelUtil<Awards> util = new ExcelUtil<Awards>(Awards.class);
        util.exportExcel(response, list, "奖项管理数据");
    }

    /**
     * 获取奖项管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:awards:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(awardsService.selectAwardsById(id));
    }

    /**
     * 新增奖项
     */
    @PreAuthorize("@ss.hasPermi('system:awards:add')")
    @Log(title = "奖项管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Awards awards) {
        awards.setUserId(getUserId());
        return toAjax(awardsService.insertAwards(awards));
    }

    /**
     * 修改奖项管理
     */
    @PreAuthorize("@ss.hasPermi('system:awards:edit')")
    @Log(title = "奖项管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Awards awards) {

        Long awardsId = awards.getAwardsId();
        AwardsRule awardsRule = awardsService.selectAwardsRuleById(awardsId);
        int isFirstPerson = awards.getIsFirstPerson();
        if (isFirstPerson == 1) {
            awards.setPoints(awardsRule.getFirstPoints());
        } else {
            awards.setPoints(awardsRule.getSecondPoints());
        }
        int i = awardsService.updateAwards(awards);
        return toAjax(i);
    }

    /**
     * 删除奖项管理
     */
    @PreAuthorize("@ss.hasPermi('system:awards:remove')")
    @Log(title = "奖项管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(awardsService.deleteAwardsByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Awards> util = new ExcelUtil<Awards>(Awards.class);
        util.importTemplateExcel(response, "奖项数据");
    }

}
